$base64map: (
        "000000": 'A',
        "000001": 'B',
        "000010": 'C',
        "000011": 'D',
        "000100": 'E',
        "000101": 'F',
        "000110": 'G',
        "000111": 'H',
        "001000": 'I',
        "001001": 'J',
        "001010": 'K',
        "001011": 'L',
        "001100": 'M',
        "001101": 'N',
        "001110": 'O',
        "001111": 'P',
        "010000": 'Q',
        "010001": 'R',
        "010010": 'S',
        "010011": 'T',
        "010100": 'U',
        "010101": 'V',
        "010110": 'W',
        "010111": 'X',
        "011000": 'Y',
        "011001": 'Z',
        "011010": 'a',
        "011011": 'b',
        "011100": 'c',
        "011101": 'd',
        "011110": 'e',
        "011111": 'f',
        "100000": 'g',
        "100001": 'h',
        "100010": 'i',
        "100011": 'j',
        "100100": 'k',
        "100101": 'l',
        "100110": 'm',
        "100111": 'n',
        "101000": 'o',
        "101001": 'p',
        "101010": 'q',
        "101011": 'r',
        "101100": 's',
        "101101": 't',
        "101110": 'u',
        "101111": 'v',
        "110000": 'w',
        "110001": 'x',
        "110010": 'y',
        "110011": 'z',
        "110100": '0',
        "110101": '1',
        "110110": '2',
        "110111": '3',
        "111000": '4',
        "111001": '5',
        "111010": '6',
        "111011": '7',
        "111100": '8',
        "111101": '9',
        "111110": '+',
        "111111": '/',
        "======": '='
);


@function insertXmlns($svg) {
  // 检查是否存在 xmlns，如果没有就插上去
  $xmlns: 'xmlns="http://www.w3.org/2000/svg"';
  $xmlns2: "xmlns='http://www.w3.org/2000/svg'";
  $insertPos: str_index($svg, "<svg");// 如果是 null，应该要报错了
  @if(null == str_index($svg, $xmlns) && null == str_index($svg, $xmlns2) && null != $insertPos) {
    // 需要插入 xmlns
    $svg: str_insert($svg, " "+$xmlns, $insertPos + 4);
  }

  @return $svg
}

// ascii码列表 -- 8位二进制
$asciiMap: (
        "	": "00001001",
        " ": "00100000",
        "!": "00100001",
        "\"": "00100010",
        "#": "00100011",
        "$": "00100100",
        "%": "00100101",
        "&": "00100110",
        "'": "00100111",
        "(": "00101000",
        ")": "00101001",
        "*": "00101010",
        "+": "00101011",
        ",": "00101100",
        "-": "00101101",
        ".": "00101110",
        "/": "00101111",
        "0": "00110000",
        "1": "00110001",
        "2": "00110010",
        "3": "00110011",
        "4": "00110100",
        "5": "00110101",
        "6": "00110110",
        "7": "00110111",
        "8": "00111000",
        "9": "00111001",
        ":": "00111010",
        ";": "00111011",
        "<": "00111100",
        "=": "00111101",
        ">": "00111110",
        "?": "00111111",
        "@": "01000000",
        "A": "01000001",
        "B": "01000010",
        "C": "01000011",
        "D": "01000100",
        "E": "01000101",
        "F": "01000110",
        "G": "01000111",
        "H": "01001000",
        "I": "01001001",
        "J": "01001010",
        "K": "01001011",
        "L": "01001100",
        "M": "01001101",
        "N": "01001110",
        "O": "01001111",
        "P": "01010000",
        "Q": "01010001",
        "R": "01010010",
        "S": "01010011",
        "T": "01010100",
        "U": "01010101",
        "V": "01010110",
        "W": "01010111",
        "X": "01011000",
        "Y": "01011001",
        "Z": "01011010",
        "[": "01011011",
        "\\": "01011100",
        "]": "01011101",
        "^": "01011110",
        "_": "01011111",
        "`": "01100000",
        "a": "01100001",
        "b": "01100010",
        "c": "01100011",
        "d": "01100100",
        "e": "01100101",
        "f": "01100110",
        "g": "01100111",
        "h": "01101000",
        "i": "01101001",
        "j": "01101010",
        "k": "01101011",
        "l": "01101100",
        "m": "01101101",
        "n": "01101110",
        "o": "01101111",
        "p": "01110000",
        "q": "01110001",
        "r": "01110010",
        "s": "01110011",
        "t": "01110100",
        "u": "01110101",
        "v": "01110110",
        "w": "01110111",
        "x": "01111000",
        "y": "01111001",
        "z": "01111010",
        "{": "01111011",
        "|": "01111100",
        "}": "01111101",
        "~": "01111110"
);

// ascii码转8位二进制
@function asciiToBin ($char: "") {
  // 从 32 到 127，不过 127 是 del。所以取值是 32~126
  // $char 的3个特殊值：-1， -2， -3
  // -1,-2 表示有两个空字符
  // -3 表示一个空字符
  @if("-1" == $char) {
    @return "0000===="; //空字符返回-表示空
  }
  @else if("-2" == $char) {
    @return "========";
  }
  @else if("-3" == $char) {
    @return "00======";
  }
  @else {
    $bin: map-get($asciiMap, $char);
    @if(null == $bin){
      $bin: map-get($asciiMap, ' '); // 当空格处理
    }
    @return $bin;
  }
}

// 将3个ascii字符转成4个base64码
@function translate3asciiTo4base64($char1, $char2, $char3) {
  $binGroup: asciiToBin($char1) + asciiToBin($char2) + asciiToBin($char3);
  // 分拆成4份
  @return getBase64Code(str_slice($binGroup, 1, 6)) + getBase64Code(str_slice($binGroup, 7, 12)) + getBase64Code(str_slice($binGroup, 13, 18)) + getBase64Code(str_slice($binGroup, 19, 24));
}

// 将6个二进制转换成 base64 编码表的索引
@function getBase64Code($bin) {
  @return map-get($base64map, $bin);
}

// 将svg字符串转换成base64数据
@function sass-svg-base64 ($svg: '') {
  $svg: insertXmlns($svg);
  $base64: 'data:image/svg+xml;base64,';
  $str_len: str_length($svg);
  $remainder: $str_len % 3;
  // 源码切片 -- 3个字符为一组
  $i: 1;
  @while $i <= $str_len - $remainder {
    $base64: $base64 + translate3asciiTo4base64(str_slice($svg, $i, $i), str_slice($svg, $i + 1, $i + 1), str_slice($svg, $i + 2, $i + 2));
    $i: $i + 3;
  }
  // 余下的1~2个字符
  @if($remainder > 0) {
    $remainderChar1: str_slice($svg, $i, $i);
    $remainderChar2: '';
    $remainderChar3: '';
    $i: $i + 1;
    @if($i <= $str_len) {
      $remainderChar2: str_slice($svg, $i, $i);
      $i: $i + 1;
      @if($i <= $str_len) {
        $remainderChar3: str_slice($svg, $i, $i);
      }
      @else {
        $remainderChar3: '-3';
      }
    }
    @else {
      $remainderChar2: '-1';
      $remainderChar3: '-2';
    }
    $base64: $base64 + translate3asciiTo4base64($remainderChar1, $remainderChar2, $remainderChar3);
  }
  @return $base64;
};
